package com.mf.controller.queue;

import com.mf.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.DelayQueue;

/**
 * @Author: lmf
 * @Create: 2025/5/22 22:27
 * @module 延迟队列使用
 */
@RestController
@RequestMapping("/delayQueue")
public class DelayQueueUse {
    @Operation(summary = "延迟队列使用")
    @GetMapping("use")
    public Result<String> use() throws InterruptedException {
        // 创建任务
        final DelayTask task = new DelayTask("任务1", 1000);
        final DelayTask task2 = new DelayTask("任务2", 2000);
        final DelayTask task3 = new DelayTask("任务3", 4000);
        final DelayTask task4 = new DelayTask("任务4", 3000);

        //  创建队列
        final DelayQueue<DelayTask> queue = new DelayQueue<DelayTask>();
        queue.put(task);
        queue.put(task2);
        queue.put(task3);
        queue.put(task4);

        // 等待获取队列中的任务
        System.out.println(queue.take());
        System.out.println(queue.take());
        System.out.println(queue.take());
        System.out.println(queue.take());
        return Result.success();
    }


}
